/**
 * 抓取数据逻辑
 */
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
const unirest = require('unirest');
const config = require('./config');
const protocol = require('./MessageActions');

let global_browser = undefined;

module.exports = (shops) => {
  // get proxy ip
  unirest.get(config.proxy_ip_pool_fetch_url).end(function (response) {
    // Except google trims the value passed :/
    let proxy_ip = response.body.toString();
    if (proxy_ip !== "no proxy!" && proxy_ip !== '') {
      console.info(` get proxy ip address: ${proxy_ip}`)
      let opts = {
        headless: false,
      }
      if (config.enable_proxy){
        opts.args = [
          `--proxy-server=${proxy_ip}`, // Or whatever the address is
        ]
      }
      puppeteer.launch(opts).then(async browser => {
        global_browser = browser;
        var shop = undefined;
        const page = await browser.newPage();
        await page.setCookie({
          "value": "f56b87e8d03e89b954fa4e36beee9f1a7c8a833165d2fe98b6b431f1252cd74a9f60c66989f65cb9d56d7ad1ddfe3af7c329ea9fb95d5784e3bf842098c6d0591fce078551a945a53b50952515271b958f1b5990901ca02ad3ea971504a3e113",
          "expires": Date.now() / 1000 + 10,
          "domain": ".dianping.com",
          "name": "dper",
          "httpOnly": true,
          "path": '/'
        });
        await page.setCookie({
          "value": "15917403918",
          "expires": Date.now() / 1000 + 10,
          "domain": ".dianping.com",
          "name": "ua",
          "path": '/'
        });
        await page.setCookie({
          "value": "15917403918",
          "expires": Date.now() / 1000 + 10,
          "domain": ".dianping.com",
          "name": "uamo",
          "path": '/'
        });
        // login page
        await page.goto('https://account.dianping.com/login?redir=http://www.dianping.com');
        await page.waitFor(3000);
        const frames = await page.frames();
        console.error(`frames len: ${frames.length}`)
        const tryItFrame = frames[1];
        const framedButton = await tryItFrame.$('.bottom-password-login');
        framedButton.click();
        await page.waitFor(2000);
        const tabButton = await tryItFrame.$('#tab-account');
        tabButton.click();
        await page.waitFor(2000);
        const accountTextbox =  await tryItFrame.$('#account-textbox')
        accountTextbox.type('15917403918');
        const passwordTextbox =  await tryItFrame.$('#password-textbox')
        passwordTextbox.type('xh151413!');
        const loginButton = await tryItFrame.$('#login-button-account');
        loginButton.click();
        await page.waitFor(2000);
        while(shop = shops.shift()){
          let p = path.join(process.cwd(), 'shop_comments', `shop_${shop.shopId}.json`);
          let p2 = path.join(process.cwd(), 'shop_infos', `shop_${shop.shopId}.json`);
          if(fs.existsSync(p) && fs.existsSync(p)){
            continue;
          }
          if (!shop){
            continue;
          }
          const writeStream = fs.createWriteStream(p);
          const writeStream2 = fs.createWriteStream(p2);
          var callback = function (res) {
            if (res.ok) {
              var req = res.request();
              if (req.url.startsWith('https://verify.meituan.com/v2/web/general_page')) {
                console.warn(`验证中心!`);
                if (false){
                  // TODO 需要切换代理
                  // 将当前店铺插入数组
                  shops.push(shop);
                  // send to master
                  process.send({
                    worker_pid : process.pid,
                    type: protocol.CHANGE_PROXY,
                    data: shops
                  });
                  setTimeout(function () {
                    process.exit(0);
                  },500);
                }

              }
              // 得分数据
              if (req.url.startsWith('http://www.dianping.com/ajax/json/shopDynamic/reviewAndStar')){

              }

              // 店铺更多数据
              if (req.url.startsWith('http://www.dianping.com/ajax/json/shopDynamic/shopTabs')) {
                res.json().then((shopInfo) => {
                  writeStream2.write(JSON.stringify(shopInfo));
                  writeStream2.end();
                  writeStream2.close();
                });
              }
              // 所有评论数据

              if (req.url.startsWith('http://www.dianping.com/ajax/json/shopDynamic/allReview')) {
                res.json().then((shopInfo) => {
                  console.warn(`所有评论数据!`);

                  if (shopInfo && shopInfo.reviewAllDOList && shopInfo.reviewAllDOList.length > 0) {
                    for (let index = 0; index < shopInfo.reviewAllDOList.length; index++) {
                      var review = shopInfo.reviewAllDOList[index];
                      let member = {
                        userId: review.user.userId.toString(),
                        userNickName: review.user.userNickName,
                        userFace: review.user.userFace,
                        userPower: review.user.userPower,
                        vipLevel: review.vipLevel
                        // datas: datas
                      };
                      let shopData = Object.assign({
                        cityName: shop.cityName,
                        dishTagStrList: shopInfo.dishTagStrList,
                        reviewCountPic: shopInfo.reviewCountPic,
                        reviewCountAll: shopInfo.reviewCountAll,
                      }, shop);
                      unirest.post(`http://127.0.0.1:7001/shop/${shop.shopId}`)
                      .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                      .send(shopData)
                      .end(function (response) {
                        console.log(response.body);
                      });
                      unirest.post('http://127.0.0.1:7001/member')
                      .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                      .send(member)
                      .end(function (response) {
                        console.log(response.body);
                      });
                      writeStream.write(JSON.stringify(shopData));
                      writeStream.end();
                      writeStream.close();
                    }
                  }else{
                    console.warn(`http://www.dianping.com/ajax/json/shopDynamic/allReview!  not data? : ${ JSON.stringify(shopInfo)}}`);

                  }
                });
              }
            }
          };
          await excute(page,shop.shopUrl, browser,callback);
          await sleep(5000);
        }
        // await browser.close();

      }).catch(err => console.error(err));
    }
    else{
      console.error(` can not gey proxy ip, Error Info: ${proxy_ip}`)
      // send to master
      process.send({
        worker_pid : process.pid,
        type: protocol.CHANGE_PROXY,
        data: shops
      });
      setTimeout(function () {
        process.exit(0);
      },500);
    }
  });
}

function sleep2(seconds){
  var waitUntil = new Date().getTime() + seconds;
  while(new Date().getTime() < waitUntil) true;
}

async function excute(page,shopUrl, browser, callback) {
  page.on('response', callback);
  await page.goto(shopUrl);
  await sleep(5000);
}
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}


process.on('exit', async() => {
  if (global_browser) {
    await global_browser.close();
  }

});





